DSAN 5200
  • Dashboard
  • Data
  • Findings
  • Conclusion
  • References
  • Appendix
Scatterplot Dashboard Code
  • select variables for scatterplot
  • create list of clean variable names
  • create a list containing the ranges of each variable
  • create a list of density estimates for each variable
  • ini
  • add scatterplot for non-zero death counties
  • create density curves for zero death vs non-zero death counties
  • 4. finalize layout

vars <- c(“total_pov”, “child_pov”, “mhi”, “rural_urban”, “pct_no_hs”, “pct_hs”, “pct_bach”, “log_census_2020_pop”, “pop_ch_rate”,“migration_rate”, “unemployment_rate_2021”)

var_labels <- c(“Poverty Rate”, “Child Poverty”, “Median Household Income”, “Urban-Rural Continuum (1 = Urban)”, “Adults w/o HS Degree”, “Adults w/ HS Degree”, “Adults w/ BA or Higher”, “Log Population”, “Population Change Rate (20-21)”,“Domestic Migration Rate (20-21)”, “Unemployment Rate”)

ranges <- lapply(vars, function(v) range(df[[v]], na.rm = TRUE))

dens_zero_list <- lapply(df_zeroes[vars], function(x) density(x, na.rm = TRUE)) dens_nz_list <- lapply(df_nonzeroes[vars], function(x) density(x, na.rm = TRUE))

fig <- plot_ly()

for(i in seq_along(vars)) { fit <- lm(df_nonzeroes$addiction_per_10000 ~ df_nonzeroes[[vars[i]]])

fig <- fig %>% add_trace( x = df_nonzeroes[[vars[i]]], y = df_nonzeroes\(addiction_per_10000, mode = "markers", text = paste(df_nonzeroes\)county_name, “County,”, df_nonzeroes\(state_name, "<br>Addiction Deaths per 10,000: ", prettyNum(round(df_nonzeroes\)addiction_per_10000, 1), big.mark = “,”)), name = var_labels[i], visible = (i == 1), legend_group = var_labels[i] ) }

for(i in seq_along(vars)) { dz <- dens_zero_list[[i]] dn <- dens_nz_list[[i]] fig <- fig %>% add_lines( x = dz\(x, y = dz\)y, name = paste0(var_labels[i], ” density (0)“), visible = FALSE, xaxis =”x2”, yaxis = “y2”, legendgroup = var_labels[i] ) %>% add_lines( x = dn\(x, y = dn\)y, name = paste0(var_labels[i], ” density (>0)“), visible = FALSE, xaxis =”x2”, yaxis = “y2”, legendgroup = var_labels[i] ) }

n <- length(vars) btns <- lapply(seq_len(n), function(i) { vis <- rep(FALSE, n + 2*n) # turn on the i-th scatter: vis[i] <- TRUE # turn on the two density traces right after the block of scatters: vis[n + (2*(i-1) + 1)] <- TRUE vis[n + (2*(i-1) + 2)] <- TRUE

list( method = “update”, args = list( list(visible = vis), list( xaxis = list(domain = c(0,0.7), title = var_labels[i]), yaxis = list(domain = c(0,0.85), title = “Addictions / 10k”), xaxis2 = list(domain = c(0.75,1), title = var_labels[i], anchor = “y2”), yaxis2 = list(domain = c(0,1), overlaying = “y”, side = “right”, showgrid = FALSE) ) ), label = var_labels[i] ) })

fig %>% layout( updatemenus = list( list( y = 1, x = 0.5, buttons = btns ) ), # initial axis setup (before any button click) xaxis = list(domain = c(0,0.7), title = var_labels[1]), yaxis = list(domain = c(0,0.85), title = “Addictions / 10k”), xaxis2 = list(domain = c(0.75,1), title = var_labels[1], anchor = “y2”), yaxis2 = list(domain = c(0,1), overlaying = “y”, side = “right”, showgr